package ren.xbcr.schoolbus.client;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import ren.xbcr.schoolbus.client.fragment.RatingDriverFragment;
import ren.xbcr.schoolbus.client.util.Tool;


/**
 * An activity representing a list of Drivers. This activity
 * has different presentations for handset and tablet-size devices. On
 * handsets, the activity presents a list of items, which when touched,
 * lead to a {@link DriverDetailActivity} representing
 * item details. On tablets, the activity presents the list of items and
 * item details side-by-side using two vertical panes.
 * <p/>
 * The activity makes heavy use of fragments. The list of items is a
 * {@link DriverListFragment} and the item details
 * (if present) is a {@link DriverDetailFragment}.
 * <p/>
 * This activity also implements the required
 * {@link DriverListFragment.Callbacks} interface
 * to listen for item selections.
 */
public class DriverListActivity extends AppCompatActivity
        implements DriverListFragment.Callbacks, RatingDriverFragment.NoticeDialogListener {

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet
     * device.
     */
    private boolean mTwoPane;

    private Button mMainMapButton;
    private Button mDriverRatingButton;
    private Button mAboutBusinessButton;
    private Button mRecruitDriverButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_app_bar);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle(getTitle());
        ActionBar actionBar = getSupportActionBar();
//        actionBar.setDisplayHomeAsUpEnabled(true);

/*        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });*/

        if (findViewById(R.id.driver_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            ((DriverListFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.driver_list))
                    .setActivateOnItemClick(true);
        }

        setupBtn();
        // TODO: If exposing deep links into your app, handle intents here.
    }



    private void setupBtn() {
        final FragmentActivity activity = this;
        mMainMapButton = (Button) findViewById(R.id.main_map_button);
        mMainMapButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(DriverListActivity.this, activity)) return;
                Intent intent = new Intent(DriverListActivity.this, MainActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });

        mDriverRatingButton = (Button) findViewById(R.id.driver_rating_btn);
        mDriverRatingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*if (Tool.navToLoginActivity(DriverListActivity.this, activity)) return;
                Intent intent = new Intent(DriverListActivity.this, DriverListActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);*/
            }
        });

        mAboutBusinessButton = (Button) findViewById(R.id.about_business_btn);
        mAboutBusinessButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(DriverListActivity.this, activity)) return;
                Intent intent = new Intent(DriverListActivity.this, BusinessCooperationActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });

        mRecruitDriverButton = (Button) findViewById(R.id.recruit_driver_btn);
        mRecruitDriverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Tool.navToLoginActivity(DriverListActivity.this, activity)) return;
                Intent intent = new Intent(DriverListActivity.this, RecruitDriverActivity.class);
                intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
                startActivity(intent);
            }
        });
    }

    /**
     * Callback method from {@link DriverListFragment.Callbacks}
     * indicating that the item with the given ID was selected.
     */
    @Override
    public void onItemSelected(String id) {
/*        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.
            Bundle arguments = new Bundle();
            arguments.putString(DriverDetailFragment.ARG_ITEM_ID, id);
            DriverDetailFragment fragment = new DriverDetailFragment();
            fragment.setArguments(arguments);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.driver_detail_container, fragment)
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, DriverDetailActivity.class);
            detailIntent.putExtra(DriverDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }*/
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
//        mHandler.post(mRunnableCode);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDialogConfirmClick(DialogFragment dialog) {

        Context context = getApplicationContext();
        CharSequence text = getString(R.string.rating_driver_message);
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

        RatingBar ratingBar = ((RatingDriverFragment) dialog).getRatingBar();
        ratingBar.setIsIndicator(true);

        TextView textView = null;
        ViewGroup row = (ViewGroup) ratingBar.getParent();
            View view = row.getChildAt(0);
            if (view instanceof ViewGroup) {
                ViewGroup textViewGroup = (ViewGroup) view; //Found it!
                view = textViewGroup.getChildAt(1);
                if (view instanceof TextView) {
                    textView = (TextView) view;
                    textView.setText(getString(R.string.is_rating));
                    textView.setVisibility(View.VISIBLE);
                }
            }
    }

    @Override
    public void onDialogCancelClick(DialogFragment dialog) {

        RatingBar ratingBar = ((RatingDriverFragment) dialog).getRatingBar();
        ratingBar.setRating(0);
    }
}
